<?php

class ContentListElementBLog
{
	/**
	 * Id of element.
	 * @var string
	 */
	protected $_id;
	
	/**
	 * Name of element.
	 * @var string
	 */
	protected $_name;

	/**
	 * Description of element.
	 * @var string
	 */
	protected $_description;
	
	/**
	 * Full text of element.
	 * @var string
	 */
	protected $_text;
	
	/**
	 * URL for preview image.
	 * @var string
	 */
	protected $_preview;

	/**
	 * Target url.
	 * @var string
	 */
	protected $_url;
	
	/**
	 * Second url.
	 * @var string
	 */
	protected $_descriptionUrl;
	
	/**
	 * Is current element.
	 * @var integer
	 */
	protected $_isCurrent;

	/**
	 * Create new ContentListElement by any class implemented IContentListElementProvider.
	 * Also store base object inside.
	 *
	 * @param IContentListElementProvider $provider
	 * @param string Abstract type of ContentListElement
	 * @return ContentListElement
	 */
	public static function fromProvider($provider, $type = null) {
		if (!$provider instanceof IContentListElementProvider) {
			throw new Exception('Intefrace IContentListElementProvider not implemented');
		}

		$element = $provider->getContentListElement($type);
		if ($element) {
			$element->_object = $provider;
			return $element;
		} else {
			return false;
		}
	}

	public static function fromRowset($rowset, $params = array())
	{
		//var_dump($rowset);
		//$rowset = new Zend_Db_Table_Rowset();
		if (!$rowset instanceof ContentlistElementRowset) {
			$peer = call_user_func(array($rowset->getTableClass(), 'getInstance'));
			if ($peer instanceof Db_Peer) {
				$list = $peer->getContentList($rowset, $params);
			}
		} else {
			$list = $rowset->getContentList($params);
		}
		if ($list) {
			return $list;
		}
		return false;
	}
	
	/**
	 * Try to call function from parent object when ContentListElement have not this function.
	 *
	 * @param string $function
	 * @return mixed
	 */
	public function __call($function, $args)
	{
		// determite if it's 'get' or 'set'
		$action = substr($function, 0, 3);
		$class = get_class($this);
		if ($action != 'get' && $action != 'set') {
			throw new Exception("Function $function is not defined in class $class");
		}
		
		// replacing 'get' by empty string		
		$varName = substr_replace($function, '', 0, 3);
		$varName[0] = strtolower($varName[0]);
		$varName = '_'.$varName;
		
		// get list of class variables
		$vars = array_keys(get_object_vars($this));

		if (!in_array($varName, $vars)) {
			throw new Exception("Variable $varName is not defined in class $class");	
		}
		
		if ($action === 'get') {
			return $this->$varName;
		} elseif ($action === 'set') {
			$this->$varName = $args[0];
		}
	}
	
	public function __get($name) {
		return $this->$name;
	}
	
	public function __set($name, $value){
		$this->$name = $value;
	}

	/**
     * Generates a URL path that can be used in URL creation, redirection, etc.
     *
     * May be passed user params to override ones from URI, Request or even defaults.
     * If passed parameter has a value of null, it's URL variable will be reset to
     * default.
     *
     * If null is passed as a route name assemble will use the current Route or 'default'
     * if current is not yet set.
     *
     * Reset is used to signal that all parameters should be reset to it's defaults.
     * Ignoring all URL specified values. User specified params still get precedence.
     *
     * Encode tells to url encode resulting path parts.
     *
     * @param  array $userParams Options passed by a user used to override parameters
     * @param  mixed $name The name of a Route to use
     * @param  bool $reset Whether to reset to the route defaults ignoring URL params
     * @param  bool $encode Tells to encode URL parts on output
     * @throws Zend_Controller_Router_Exception
     * @return string Resulting URL path
     */
	protected function _getUrl($userParams, $name = null, $reset = false, $encode = true)
	{
		$front = Zend_Controller_Front::getInstance();
		$router = $front->getRouter();
		return $router->assemble($userParams, $name, $reset, $encode);
	}
}